/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.Constant;
import com.apache.bid.common.WbdToolsUtil;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItemGroup;
import com.apache.bid.entity.BidPub;
import com.apache.bid.entity.BidSession;
import com.apache.bid.manager.BidItemGroupManager;
import com.apache.bid.manager.BidPubManager;
import com.apache.bid.manager.BidSessionManager;
import com.apache.cache.util.Validator;

/**
 * description:  统一下保存活动插件
 * @author Hou Dayu 创建时间：2016-3-23  
 * 
 */
public class UnitySaveSessionPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidSessionManager bidSessionManager;
	protected BidPubManager bidPubManager;
	protected BidItemGroupManager bidItemGroupManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		String sessionId = saveSession(param);
		if (Validator.isNotNull(sessionId)) {
			entity.setResult("true");
			entity.setMessage("保存成功");
			entity.setEntity(sessionId);
		} else {
			entity.setResult("false");
			entity.setMessage("保存失败");
		}
		return entity;
	}

	private String saveSession(ParamsVo param) {
		String sessionId = bidSessionManager.saveInfo(param);
		if (Validator.isNotNull(sessionId)) {
			BidPub pub = new BidPub();
			pub = (BidPub) WbdToolsUtil.getInstance().convert2Bean(param.getParams(), pub);
			pub.setSessionId(sessionId);
			ParamsVo<BidPub> pubVo = new ParamsVo<BidPub>();
			pubVo.setObj(pub);
			String pubId = bidPubManager.saveInfo(pubVo);
			//初始化分组表
			BidItemGroup itemGroup = new BidItemGroup();
			itemGroup = (BidItemGroup) WbdToolsUtil.getInstance().convert2Bean(param.getParams(), itemGroup);
			itemGroup.setSessionId(sessionId);
			itemGroup.setGroupCode(Validator.getDefaultStr(itemGroup.getGroupCode(), "default"));
			itemGroup.setGroupName(Validator.getDefaultStr(itemGroup.getGroupName(), "默认分组"));
			ParamsVo<BidItemGroup> groupVo = new ParamsVo<BidItemGroup>();
			groupVo.setObj(itemGroup);
			String groupId = bidItemGroupManager.saveInfo(groupVo);
			log.info("保存完成：sessionId=" + sessionId + ",pubId=" + pubId + ",groupId=" + groupId);
			BidSession bidSession = (BidSession) param.getObj();
			//以下是缓存处理
			if (Constant.getInstance().getSessionConfig("statusPubslish").equals(bidSession.getStatus())) {
				BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("session");
				boolean mark = cache.createCacheObject(bidSession);
				if (mark) {
					cache.createCacheObject(cache.pub_prefix + sessionId, pub);
				}
			}
		}
		return sessionId;
	}

	public void setBidSessionManager(BidSessionManager bidSessionManager) {
		this.bidSessionManager = bidSessionManager;
	}

	public void setBidPubManager(BidPubManager bidPubManager) {
		this.bidPubManager = bidPubManager;
	}

	public void setBidItemGroupManager(BidItemGroupManager bidItemGroupManager) {
		this.bidItemGroupManager = bidItemGroupManager;
	}

}
